package com.example.recyclerviewtest

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView


class CatAdapter(val catList: List<Cat>): RecyclerView.Adapter<CatAdapter.ViewHolder>() {
    inner class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        val catImage: ImageView = view.findViewById(R.id.catImage)
        val catName: TextView = view.findViewById(R.id.catName)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.cat_item,parent,false)
        val viewHolder = ViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.adapterPosition
            val cat = catList[position]
            Toast.makeText(parent.context,"You checked view ${cat.name}",
                Toast.LENGTH_SHORT).show()
        }
        viewHolder.catImage.setOnClickListener {
            val position = viewHolder.adapterPosition
            val cat = catList[position]
            Toast.makeText(parent.context,"You checked image ${cat.name}",
                Toast.LENGTH_SHORT).show()
        }
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val cat = catList[position]
        holder.catImage.setImageResource(cat.imageId)
        holder.catName.text = cat.name
    }

    override fun getItemCount() = catList.size

    }
